//
//  StatusCell.swift
//  HMWeibo
//
//  Created by wulei on 15/12/11.
//  Copyright © 2015年 wulei. All rights reserved.
//

import UIKit
import SnapKit

let StatusCellMargin: CGFloat = 12
let StatusCellIconWidth: CGFloat = 35
class StatusCell: UITableViewCell {
    
    // MARK:- 视图模型
    var ViewModel: HMStatusViewModel? {
        
        didSet{
            topView.ViewModel = ViewModel
            contentLabel.text = ViewModel?.status.text
            pictureView.ViewModel = ViewModel
            
            pictureView.snp_updateConstraints { (make) -> Void in
                make.height.equalTo(pictureView.bounds.height)
                make.width.equalTo(pictureView.bounds.width)
            }

            
        }
    }
    
    // MARK:- 计算tableView 的行高
    func tableCellRowHeight(vm: HMStatusViewModel) -> CGFloat {
        ViewModel = vm
        contentView.layoutIfNeeded()
        return CGRectGetMaxY(bottomView.frame)
    }

    // MARK:- 构造函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        selectionStyle = .None

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK:- 懒加载Cell 内部的控件
     lazy var topView: StatusCellTopView = StatusCellTopView()
     lazy var contentLabel: UILabel = UILabel(title: "微博正文", fontSize: 15, color: UIColor.darkGrayColor(), screenInset: StatusCellMargin)
     lazy var pictureView: StatusCellPictureView = StatusCellPictureView()
     lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
    
    //
    
}


// MARK:- 设置界面
extension StatusCell {
    
    func setupUI() {
        topView.backgroundColor = UIColor(white: 0.85, alpha: 1.0)
        bottomView.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
        
        // 1.添加控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        // 2.自动布局
        // 顶视图
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            // TODO: - 修改高度
            make.height.equalTo(2 * StatusCellMargin + StatusCellIconWidth)
            
        }
        
        // 中间文字
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left).offset(StatusCellMargin)

        }
        

        // 底视图
        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(pictureView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
            
            //指定向下的约束
//            make.bottom.equalTo(contentView.snp_bottom)
        }
    
    }
    
    
    
    
}